package borge.web.teamspirit.client.page.team.widgets;

import borge.web.teamspirit.client.SessionData;
import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler.WidgetEventType;
import borge.web.teamspirit.client.page.shared.widgets.BaseWidget;
import borge.web.teamspirit.client.page.team.TokenData;
import borge.web.teamspirit.client.servercomm.ServerComm;

import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MenuWidget extends BaseWidget {

	private Tree iMenuTree;
	private VerticalPanel iPanel;
	//private String iLastClickedItem; // TEMP SOLUTION
	
	// 4 menu trees? general, sport related, personal, admin
	// some menus only shown if logged in
	
	private String[] iMenuItems = {
			"News",
			"Forum",
			"Statistics",
			"TestCreate",
			"TestPrint"
			
	};
	
	private String[] iMenuTokens = {
			TokenData.assembleTeamToken(TokenData.SHOW, TokenData.NEWS, SessionData.sCurrentTeam.getEncodedKey()),
			TokenData.assembleTeamToken(TokenData.SHOW, TokenData.FORUM),
			TokenData.assembleTeamToken(TokenData.SHOW, TokenData.STATISTICS),
			TokenData.TEST_CREATE,
			TokenData.TEST_PRINT
			
	};
	
	private String[] iAdminItems = {
			//TeamSpirit.getConstants().create_new_team(),
			//TeamSpirit.getConstants().create_new_user(),
	        TeamSpirit.getConstants().create_new_player(),
	        TeamSpirit.getConstants().create_new_serie(),
	        //TeamSpirit.getConstants().create_new_match(),
	        //TeamSpirit.getConstants().list_teams(),
	        //TeamSpirit.getConstants().list_users(),
	        TeamSpirit.getConstants().list_players(),
	        TeamSpirit.getConstants().list_series(),
	        //TeamSpirit.getConstants().list_matches()
	};
	
	private String[] iAdminTokens = {
		TokenData.assembleTeamToken(TokenData.CREATE, TokenData.PLAYER),
		TokenData.assembleTeamToken(TokenData.CREATE, TokenData.SERIE),
		TokenData.assembleTeamToken(TokenData.LIST, TokenData.PLAYER),
		TokenData.assembleTeamToken(TokenData.LIST, TokenData.SERIE)
			
	};
	
	private String[] iPersonalItems = {
			"My profile",
			"My settings",
			"My statistics"
	};
	
	private String iMenuName = TeamSpirit.getConstants().menu();
	private String iAdminName = "Admin";
	private String iPersonalName = "My stuff";
	private String iLastClickedItem = "";
	
	public MenuWidget(WidgetEventHandler aEventHandler){
		
	    super(aEventHandler);
	    

		iPanel = new VerticalPanel();

		for(int i = 0; i < iMenuItems.length; ++i){
			iPanel.add(createMenuLink(iMenuItems[i], iMenuTokens[i]));
		}
		
		for(int i = 0; i < iAdminItems.length; ++i){
			iPanel.add(createMenuLink(iAdminItems[i], iAdminTokens[i]));
		}
		
	    
	    
	    initWidget(iPanel);
	    
	}
	
	private Hyperlink createMenuLink(String aText, String aToken){
		
		Hyperlink link = new Hyperlink(aText, aToken);
		link.setStyleName("menulink");
		return link;
	}
	
	public String getClickedLinkText(){
	    return iLastClickedItem;
	}
	
	public void clearClickHistory(){
	    iLastClickedItem = "";
	}
	
	private class MySelectionHandler implements SelectionHandler<TreeItem> {
		
		public void onSelection(SelectionEvent<TreeItem> event){
		 
			//Window.alert("selected");
			if(event.getSelectedItem().getText().equals("TestCreate")){
				ServerComm.testCreate();
				return;
			}
			else if(event.getSelectedItem().getText().equals("TestPrint")){
				ServerComm.testPrint();
				return;
			}
			
		    // TODO:
		    // create a class menu to represent menu data
		    // let each tree item map to the widget that should be displayed
		    
			//TreeItem item = event.getSelectedItem();
			//Window.alert("Selected: " + item.getText());
			
			// TEMP SOLUTION
			if(!iLastClickedItem.equals(event.getSelectedItem().getText())){
				iLastClickedItem = event.getSelectedItem().getText();
				History.newItem(event.getSelectedItem().getText());
				postEvent(WidgetEventType.MENU_CLICKED);
			}
		}
		
	}
	
}
